package com.林子祺.全名健康管理系统.view.servlet.content.article;

import com.林子祺.全名健康管理系统.controller.content.article.ArticleIndexDisp;
import com.林子祺.全名健康管理系统.controller.content.comment.CommentDisp;
import com.林子祺.全名健康管理系统.entity.article.content.article.ArticleData;
import com.林子祺.全名健康管理系统.entity.article.content.article.CurrentIndexData;
import com.林子祺.全名健康管理系统.entity.article.content.comment.CommentData;
import com.林子祺.全名健康管理系统.entity.article.content.comment.CurrentCommentData;
import com.林子祺.全名健康管理系统.util.CommentStructure;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

//显示文章
@WebServlet("/display")
public class ArticleDisplay extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String name = req.getParameter("reqArticle");
        HttpSession session = req.getSession();
        String state = (String) session.getAttribute("work");
        System.out.println(state);

        ArticleData.Refresh();
        ArticleIndexDisp.pull();
        CommentData.Refresh();
        CommentDisp.pull(name);

        int number = 0;
        String Article = null;
        String Writer = null;
        int Like = 0;
        while (ArticleData.OutputArticleData(number)) {
                if (name.equals(CurrentIndexData.name)) {
                    Article = CurrentIndexData.text;
                    Writer = CurrentIndexData.writer;
                    Like = CurrentIndexData.like;
                    number++;
                } else
                    number++;
        }

        number = 0;
        ArrayList<CommentStructure> comment = new ArrayList<>();
        while(CommentData.OutputCommentData(number)){
            comment.add(new CommentStructure(CurrentCommentData.username,CurrentCommentData.articleName,CurrentCommentData.comment));
            number++;
        }

        req.setAttribute("reqArticle", name);
        req.setAttribute("article", Article);
        req.setAttribute("writer", Writer);
        req.setAttribute("like", Like);
        req.setAttribute("Comment",comment);
        req.setAttribute("state",state);
        req.getRequestDispatcher("/content/display.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
